package com.supermap.dataservice.cloud.push.adv;

import com.supermap.dubbo.api.cloud.push.Notice;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description 咨询推送显示
 * @date 2018年11月22 上午 11:24
 * @email 1301584033@qq.com
 */
@Mapper
public interface PushMapper {
    /**
     * 查询对应项目推送功能
     * */
    @Select("select group_concat(id) from t_noticetype_product where productId=#{productId}")
    String getProductNoticeTypes(@Param("productId")String productId);
    /**
     * 获取所有设备信息
     * */
    @Select("select code from t_notice_code where userId in (${users}) ")
    List<String> getUserCode(@Param("users")String users);
    /**
     * 获取所有用户信息
     * */
    @Select("select id from t_user where areaCode like concat(#{areaCode},'%')")
    List<String> getAllUserIdByAreaCode(@Param("areaCode")String areaCode);

    @Select("select group_concat(distinct t.messageid) from t_notice t where t.receiveuserid = #{receiveuserid} and t.noticetype = #{noticetype}  group by receiveuserid\n")
    String getNoticeObjectUseridList(@Param("receiveuserid")Integer receiveuserid,@Param("noticetype")Integer noticetype);

    /**
     * 获取所有未读信息总数
     * */
    @Select("select count(t1.id) num  from t_notice t1\n" +
            "where t1.receiveuserid = #{receiveuserid} and t1.isread = 0 and  if(isnull(#{noticetype}),true,find_in_set(t1.noticetype,#{noticetype}))  ")
    int getReceiveuserIdCount(@Param("receiveuserid") String receiveuserid,@Param("noticetype")String noticetype);


    /**
     * 每一类未读信息
     * */
    @Select("select noticetype, count(t1.id) num  from t_notice t1\n" +
            "where t1.receiveuserid = #{receiveuserid} and t1.isread = 0 and    if(isnull(#{noticetype}),true,find_in_set(t1.noticetype,#{noticetype}))    \n" +
            "group by t1.noticetype;")
    List<Map<String,Object>> getGroupReceiveuserIdCount(@Param("receiveuserid")String receiveuserid,@Param("noticetype")String noticetype);


    /***
     *
     *  某一类未读信息列表
     */
    @Select("select t1.id as noticeId,t1.noticetitle,t1.messageid,t1.noticetype,t1.uploadtime,t1.senduserid  from t_notice t1\n" +
            "where t1.receiveuserid = #{receiveuserid} and find_in_set(t1.noticetype,#{noticetype}) and t1.isread = 0\n" +
            "order by t1.createtime desc limit #{pageNum},#{pageSize}")
    List<Map<String,Object>> getNoticetypeReceiveuserIdList(@Param("receiveuserid")String receiveuserid,@Param("noticetype")String noticetype,@Param("pageNum")Integer pageNum,@Param("pageSize")Integer pageSize);

    /**
     * 设置已读
     * */
    @Update("update t_notice set isread=1,uploadtime=now()   where id=#{noticeId}")
    int putNoticeRead(@Param("noticeId")Integer noticeId);

    /**
     * 入库
     * */
    @Insert("insert into t_notice(noticetype,noticetitle,messageid,senduserid,receiveuserid)" +
            " values(#{notice.noticetype},#{notice.noticetitle},#{notice.messageid},#{notice.senduserid}," +
            "#{notice.receiveuserid})")
    int putNotice(@Param("notice") Notice notice);

    /**
     * 更新设备信息
     * */
    @Insert("insert ignore into t_notice_code(code,userId) values(#{code},#{userId})")
    int putNoticeCode(@Param("code")String code,@Param("userId")Integer userId);

    /**
     * 查询设备信息
     * **/
    @Select("select  code from  t_notice_code where userId=#{userId}")
    String getNoticeCodeByUserId(@Param("userId")Integer userId);


    /**
     *
     * 多条消息入库
     *
     * */
    @InsertProvider(type = PushMapperSQL.class,method = "putNoticeListSQL")
    Integer putNoticeList(@Param("noticeList")List<Notice> noticeList);

    /*
    *
    * **/
    @SelectProvider(type = PushMapperSQL.class,method = "getNoticeListSQL")
    List<Map<String,Object>> getNoticeList(@Param("noticetype") String noticetype,@Param("userId") String userId,@Param("pageSize") Integer pageSize);
    class  PushMapperSQL{

        public String getNoticeListSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String noticetype =(String)map.get("noticetype");
            String userId =(String)map.get("userId");
            Integer pageSize =(Integer)map.get("pageSize");

            stringBuilder.append("select t.id,t.noticetype,t.noticetitle,t.messageid,t.senduserid,(select showname as username from t_user where id=t.senduserid) as sendUserName,t.isread,date_format(t.createtime,'%Y-%m-%d %H:%i:%S') as createtime from t_notice  t where 1=1 ");
            if(noticetype!=null){
                stringBuilder.append(" and find_in_set(t.noticetype,'"+noticetype+"') ");
            }
            if(userId!=null){
                stringBuilder.append(" and t.receiveuserid="+userId+" ");
            }
            stringBuilder.append("order by t.createtime desc ");
            if(pageSize!=null){
                stringBuilder.append(" limit "+pageSize+" ");
            }
            return  stringBuilder.toString();
        }

        /**
         * 多条消息入库
         * */
        public String putNoticeListSQL(Map<String,Object> map){
            List<Notice> noticeList= (List<Notice>)map.get("noticeList");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" insert into t_notice(noticetype,noticetitle,messageid,senduserid,receiveuserid) ");
            stringBuilder.append(" values ");
            if(noticeList!=null&&noticeList.size()>0){
                for (Notice notice:noticeList){
                    stringBuilder.append(" ("+notice.getNoticetype()+"" +
                            ",'"+notice.getNoticetitle()+"',"+notice.getMessageid()+"," +
                            "'"+notice.getSenduserid()+"','"+notice.getReceiveuserid()+"'),");
                }
            }
            return stringBuilder.substring(0,stringBuilder.lastIndexOf(",")).toString();
        }

    }



}
